#include "formqdtest.h"
#include "ui_formqdtest.h"

#include <QJsonArray>
#include <QJsonObject>
#include <QJsonDocument>
#include <QMessageBox>

FormQDTest::FormQDTest(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::FormQDTest)
{
    ui->setupUi(this);
    setWindowFlags(windowFlags() | Qt::Dialog);
    setWindowModality(Qt::ApplicationModal);
    setAttribute(Qt::WA_DeleteOnClose);

    mqtt.mqttConnect();
    mqtt.subscribe("inner");

    connect(&mqtt, &MQTT_IPC::recvMsg, this, &FormQDTest::on_recvMQTTMsg);

    qDebug() << "[FormQDTest] \t Constructor";
}

FormQDTest::~FormQDTest()
{
    mqtt.mqttDisconnect();
    delete ui;
    qDebug() << "[FormQDTest] \t Destructor";
}

void FormQDTest::on_pushButtonQDTestReturn_clicked()
{
    this->close();
}

void FormQDTest::on_pushButtonTurnOn_clicked()
{
    mqtt.sendMsg("inner", "{ \
                 \"src\": \"dcsmini\", \
                 \"dst\": [\"excqdapp\"], \
                 \"payload\": { \
                     \"cmd\": \"qd-all-on\", \
                     \"sn\": 123 \
                 }}");
}

void FormQDTest::on_pushButtonTurnOff_clicked()
{
    mqtt.sendMsg("inner", "{ \
                 \"src\": \"dcsmini\", \
                 \"dst\": [\"excqdapp\"], \
                 \"payload\": { \
                     \"cmd\": \"qd-all-off\", \
                     \"sn\": 123 \
                 }}");
}

void FormQDTest::on_pushButtonAddrAlloc_clicked()
{
    mqtt.sendMsg("inner", "{ \
                 \"src\": \"dcsmini\", \
                 \"dst\": [\"excqdapp\"], \
                 \"payload\": { \
                     \"cmd\": \"qd-autoaddr-set\", \
                     \"sn\": 123 \
                 }}");
}

void FormQDTest::on_recvMQTTMsg(const QByteArray &msg)
{
    QJsonParseError err_rpt{};
    QJsonDocument root_Doc = QJsonDocument::fromJson(msg, &err_rpt); // 字符串格式化为JSON

    if (err_rpt.error != QJsonParseError::NoError)
    {
        qDebug() << "JSON格式错误";
    }
    else
    {
        QString srcapp, dstapp;
        QJsonValue srcValue, dstValue, payloadValue;
        QJsonObject root_Obj = root_Doc.object();

        srcValue = root_Obj.value("src");
        if (srcValue.isString())
        {
            srcapp = srcValue.toString();
        }

        dstValue = root_Obj.value("dst");
        if (dstValue.isArray())
        {
            dstapp = dstValue.toArray().at(0).toString();
        }

        if (srcapp == "excqdapp" && dstapp == "dcsmini")
        {
            payloadValue = root_Obj.value("payload");
            if (payloadValue.isObject())
            {
                QString cmdStr = payloadValue.toObject().value("cmd").toString();
                if (cmdStr == "qd-autoaddr-set")
                {
                    quint32 addrnum = payloadValue.toObject().value("addrnum").toInt();
                    if (addrnum == 0)
                    {
                        QMessageBox::warning(this, tr("自动地址分配"), tr("自动地址分配失败"), QMessageBox::Ok);
                    }
                    else
                    {
                        QMessageBox::information(this, tr("自动地址分配"), tr("自动地址分配成功"), QMessageBox::Ok);
                    }
                }
                else if (cmdStr == "qd-all-on")
                {
                    QString retStr = payloadValue.toObject().value("result").toString();
                    if (retStr == "OK")
                    {
                        QMessageBox::information(this, tr("全开"), tr("全开操作成功"), QMessageBox::Ok);
                    }
                    else
                    {
                        QMessageBox::warning(this, tr("全开"), tr("全开操作失败"), QMessageBox::Ok);
                    }
                }
                else if (cmdStr == "qd-all-off")
                {
                    QString retStr = payloadValue.toObject().value("result").toString();
                    if (retStr == "OK")
                    {
                        QMessageBox::information(this, tr("全关"), tr("全关操作成功"), QMessageBox::Ok);
                    }
                    else
                    {
                        QMessageBox::warning(this, tr("全关"), tr("全关操作失败"), QMessageBox::Ok);
                    }
                }
            }
        }
    }
}
